<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <div id="root">
        <!--让标题的透明度从1逐渐减到0，再变回1重复-->
        <h2 v-bind:style="{opacity: opacity}">欢迎学习vue</h2>
        <button @click="stop">点我停止变换</button>
    </div>
 
    <script>
        Vue.config.productionTip = false
        let vm = new Vue({
            el: "#root",
            data: {
                opacity: 1,
            },
            methods: {
                stop() {
                    //实际中一般不会主动调用$destroy，即一般是意外情况导致vm销毁，所以销毁之前的收尾操作要写到beforeDestroy函数中
                    this.$destroy();
                }
            },
            mounted() {
                console.log("mounted", this) //这里this也是vm\
                //往vm上存定时器id
                this.timerId = setInterval(()=>{
                    this.opacity -= 0.01
                    if(this.opacity <=0) {
                        this.opacity = 1
                    }
                }, 16) 
            },
            beforeDestroy() {
                console.log('vm要死了')
                //销毁之前清除定时器
                clearInterval(this.timerId)
            },
        })

    </script>
</body>
</html>